home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / benchmarks / cswitch / RCS / cswitch.c,v < prev   
Encoding:
Text File  |  1989-08-31  |  2.3 KB  |  106 lines

  1. head     1.1;
  2. branch   ;
  3. access   ;
  4. symbols  ;
  5. locks    ; strict;
  6. comment  @ * @;
  7.  
  8.  
  9. 1.1
  10. date     89.08.31.13.19.37;  author ouster;  state Exp;
  11. branches ;
  12. next     ;
  13.  
  14.  
  15. desc
  16. @@
  17.  
  18.  
  19.  
  20. 1.1
  21. log
  22. @Initial revision
  23. @
  24. text
  25. @/* 
  26.  * cswitch.c --
  27.  *
  28.  *    Simple program to test context switching by sending short
  29.  *    messages back and forth through a pipe.  Invocation:
  30.  *    
  31.  *    cswitch count
  32.  *
  33.  *    Count tells how many times a one-byte message should be sent
  34.  *    back and forth between two processes.
  35.  *
  36.  * Copyright 1987, 1989 Regents of the University of California
  37.  * Permission to use, copy, modify, and distribute this
  38.  * software and its documentation for any purpose and without
  39.  * fee is hereby granted, provided that the above copyright
  40.  * notice appear in all copies.  The University of California
  41.  * makes no representations about the suitability of this
  42.  * software for any purpose.  It is provided "as is" without
  43.  * express or implied warranty.
  44.  */
  45.  
  46. #ifndef lint
  47. static char rcsid[] = "$Header: protoPub.c,v 1.3 87/01/04 17:28:56 andrew Exp $ SPRITE (Berkeley)";
  48. #endif not lint
  49.  
  50. #include <stdio.h>
  51. #include <sys/time.h>
  52.  
  53. main(argc,argv)
  54.     int argc;
  55.     char **argv;
  56. {
  57.     int count, i, toSlave[2], fromSlave[2];
  58.     int pid;
  59.     char buffer[10];
  60.     struct timeval begin ,end;
  61.     int micros;
  62.     double timePer;
  63.     extern int errno;
  64.  
  65.     if (argc != 2 ) {
  66.     fprintf(stderr, "Ping takes one argument: count.\n");
  67.     return;
  68.     }
  69.     count = 0;
  70.     sscanf(argv[1], "%d", &count);
  71.     pipe(toSlave);
  72.     pipe(fromSlave);
  73.     pid = fork();
  74.     if (pid == 0) {
  75.     write(fromSlave[1], "a", 1);
  76.     while (1) {
  77.         read(toSlave[0], buffer, 1);
  78.         if (buffer[0] != 'a') {
  79.         exit(0);
  80.         }
  81.         write(fromSlave[1], "a", 1);
  82.     }
  83.     }
  84.     if (pid == -1) {
  85.     fprintf(stderr, "Couldn't fork slave process: error %d.\n",
  86.         errno);
  87.     return;
  88.     }
  89.     read(fromSlave[0], buffer, 1);
  90.     gettimeofday(&begin, (struct timezone *) NULL);
  91.     for (i = 0; i < count; i++) {
  92.     write(toSlave[1], "a", 1);
  93.     read(fromSlave[0], buffer, 1);
  94.     }
  95.     gettimeofday(&end, (struct timezone *) NULL);
  96.     micros = 1000000*(end.tv_sec - begin.tv_sec)
  97.         + end.tv_usec - begin.tv_usec;
  98.     write(toSlave[1], "b", 1);
  99.     timePer = micros;
  100.     timePer /= count * 1000;
  101.  
  102.     printf("Elapsed time per ping (2 context switches): %.2f milliseconds.\n",
  103.         timePer);
  104. }
  105. @
  106.